<?php
/**
 * 商品模型
 *
 *
 *
 *
 * @copyright  Copyright (c) 中国追溯
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');
class goods_historyModel extends Model{

    public function __construct(){
        parent::__construct('goods_history');
    }

    /**
     * 得到商品历史中极限的价格(max.min)
     * @param  integer $goods_id 商品id
     * @return  array / false
     */
    public function getGoodsHistoryExtremity($goods_id = 0)
    {
        $map = array(
            'goods_id' => (int)$goods_id
        );
        $extremity_data = $this->table('goods_history')->field('max(goods_promotion_price) AS max_price,min(goods_promotion_price) AS min_price')->where($map)->find();
        return $extremity_data ? $extremity_data : false;
    }

    /**
     * 读取列表
     * @param array $condition
     *
     */
    public function getHistoryLogList($condition, $page='', $order='', $field='*') {
        $result = $this->field($field)->where($condition)->page($page)->order($order)->select();
        return $result;
    }

    /**
     * 读取单条记录
     * @param array $condition
     *
     */
    public function getHistoryLogInfo($condition) {
        $result = $this->where($condition)->find();
        return $result;
    }

    /**
     * 数据增加格式
     * @param array $add_data 增加数据
     */
    public function addDataFormat($add_data, $goodsid)
    {
        $result_data = array();
        if (empty($goodsid)) {
            $dispose_result = $this->disposeData($add_data);
            if ($dispose_result !== false) $result_data[] = $dispose_result;
        } else {
            $goodsid = (array)$goodsid;
            foreach ($goodsid as $g_id) {
                $add_data['goods_id'] = $g_id;
    ;           $dispose_result = $this->disposeData($add_data);
                if ($dispose_result !== false) $result_data[] = $dispose_result;
            }
        }

        return $result_data;
    }

    /**
     * 处理数据
     * @param  array $dispose_data 需要处理的数据
     * @return array / false
     */
    public function disposeData($dispose_data)
    {
        $result_data = [];
        if (!isset($dispose_data['goods_id']) || empty($dispose_data['goods_id'])) return false;
        $result_data['goods_id'] = (int)$dispose_data['goods_id'];
        $dispose_key = array_keys($dispose_data);
        //处理数据中是否存在，必要的键值
        $key_exist = array_intersect($dispose_key, ['goods_price', 'goods_promotion_price', 'goods_marketprice']);
        if (empty($key_exist)) return false;

        //找出当前商品相应数据
        $goods_data = $this->table('goods')->field('goods_name,goods_price,goods_promotion_price,goods_promotion_type,goods_marketprice')->where(['goods_id' => $result_data['goods_id']])->find();
        if (empty($goods_data)) return false;
        //切换回表
        $this->table('goods_history');

        $result_data['goods_price']           = isset($dispose_data['goods_price']) ? $dispose_data['goods_price'] : $goods_data['goods_price'];
        $result_data['goods_promotion_price'] = (isset($dispose_data['goods_promotion_price']) && is_numeric($dispose_data['goods_promotion_price'])) ? $dispose_data['goods_promotion_price'] : $result_data['goods_price'];
        $result_data['goods_promotion_type']  = isset($dispose_data['goods_promotion_type']) ? (int)$dispose_data['goods_promotion_type'] : $goods_data['goods_promotion_type'];
        $result_data['goods_marketprice']     = isset($dispose_data['goods_marketprice']) ? $dispose_data['goods_marketprice'] : $goods_data['goods_marketprice'];
        $result_data['create_time']           = TIMESTAMP;
        return $result_data;
    }

    /*
     * 增加
     * @param array $param
     * @param array/int $goodsid
     * @return bool
     */
    public function addHistoryLog($param, $goodsid){
        $param = $this->addDataFormat($param, $goodsid);
        // Shopnc\Log::record('model执行时间：' . date('Y-m-d H:i:s', time()), Shopnc\Log::RUN);
        if (($param === false) || empty($param)) return false;
        return $this->insertAll($param);
    }

    /*
     * 删除
     * @param array $condition
     * @return bool
     */
    public function delHistoryLog($condition){
        return $this->where($condition)->delete();
    }

}
